package ajax.quanxuan.sys.controller;


import ajax.quanxuan.sys.dao.DeptMapper;
import ajax.quanxuan.sys.entity.Dept;
import ajax.quanxuan.sys.service.DeptService;
import ajax.quanxuan.sys.utils.SystemConstant;
import ajax.quanxuan.vo.DataGridViewResult;
import ajax.quanxuan.vo.DeptVo;
import ajax.quanxuan.vo.JSONResult;
import ajax.quanxuan.vo.TreeNode;
import com.alibaba.fastjson.JSON;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author KazuGin
 * @since 2024-07-07
 */
@RestController
@RequestMapping("/sys/dept")
public class DeptController {
    @Resource
    private DeptService deptService;


    @RequestMapping("loadDeptTreeLeft")
    public DataGridViewResult loadDeptTreeLeft(Dept dept) {
        //查询所有部门
        List<Dept> deptList = deptService.list1(dept);
        //创建节点集合
        List<TreeNode> treeNodes = new ArrayList<TreeNode>();
        //循环所有部门
        for (Dept dept1 : deptList) {
            //如果状态为1  节点则为展开状态
            Boolean spread = dept.getOpen() == SystemConstant.OPEN_TRUE ? true : false;
            //封装树节点
            TreeNode treeNode = new TreeNode();
            treeNode.setId(dept1.getId());
            treeNode.setPid(dept1.getPid());
            treeNode.setTitle(dept1.getTitle());
            treeNode.setSpread(spread);
            //将树节点对象添加到节点集合
            treeNodes.add(treeNode);
        }
        //将树节点集合返回
        return new DataGridViewResult(treeNodes);
    }

    @RequestMapping("/deptlist")
    public DataGridViewResult deptlist(DeptVo deptVo, Dept dept) {
        //PageHelper.startPage(1, 10);

        // 不能把分页写死
        //给一个起始值  和每条数据转入mysql
        Long count = Long.valueOf(deptService.count(dept));

        return new DataGridViewResult(count, deptService.list(dept));
    }

    @RequestMapping("addDept")
    public JSONResult addDept(Dept dept) {
        dept.setCreatetime(new Date());
        if (deptService.save(dept)) {
            return SystemConstant.ADD_SUCCESS;
        }
        return SystemConstant.ADD_ERROR;

    }

    @RequestMapping("updateDept")
    public JSONResult updateDept(Dept dept) {
        if (deptService.updateById(dept)) {
            return SystemConstant.UPDATE_SUCCESS;
        }
        return SystemConstant.UPDATE_ERROR;

    }

    /**
     * 检查当前部门下是否存在子节点
     *
     * @param id
     * @return
     */

    @RequestMapping("checkDeptHasChildren")
    public String checkDeptHasChildren(int id,Dept dept) {
        Map<String, Object> map = new HashMap<String, Object>();

        //统计部门数量
            Integer count = deptService.count(dept);
        if (count > 0) {
            //存在子节点
            map.put(SystemConstant.EXIST, true);
            map.put(SystemConstant.MESSAGE, "对不起,当前部门下有子节点,无法删除!");
        } else {
            //不存在子节点
            map.put(SystemConstant.EXIST, false);
        }
        //将map集合转换成json
        return JSON.toJSONString(map);
    }

    /**
     * 删除部门
     *
     * @param id
     * @return
     */
    @RequestMapping("deleteById")
    public JSONResult deleteById(int id) {

        if (deptService.removeById(id)) {
            return SystemConstant.DELETE_SUCCESS;
        } else {
            return SystemConstant.DELETE_ERROR;
        }
    }
}

